﻿
function main() {
    var testVersion = "(201901021555)版";
    var softwareName = "抖音";
    var view = RequestEngine.getWebView();
    var viewJson = JSON.parse(view);
    var sendMessage = viewJson.sendMessage;
    var search = viewJson.search;
    var issixin=viewJson.issixin;
    var fenSiNum=viewJson.fenSiNum;
    var fanyeNum=viewJson.fanyeNum;
    var waitingTime=viewJson.waitingTime;

    var fenSiNumF=0;

    AccessibilityEngine.sleepEngine(2000);//等待
	if (AccessibilityEngine.checkUpId(widget.pages.shouye.wzdl) == ISTRUE) { //我知道了
        AccessibilityEngine.mockOnClickId(widget.pages.shouye.wzdl);
		}
    var jixing = getXYByIdIndex(widget.pages.shouye.wode);

   
    AccessibilityEngine.sleepEngine(10000);//等待
    AccessibilityEngine.mockOnClickId(widget.pages.shouye.shousuo); // 点击搜索 放大镜
    if (AccessibilityEngine.checkUpId(widget.pages.shouye.wzdl) == ISTRUE) { //我知道了
        AccessibilityEngine.mockOnClickId(widget.pages.shouye.wzdl);
		}
    if( jixing == 1 ){
        ScreenEngine.swipe(parseX(300), parseY(100), parseX(300), parseY(100), 500); //点击
    } else if( jixing == 2 ){
        ScreenEngine.swipe(parseX(400), parseY(100), parseX(400), parseY(100), 500); //点击
    }

    AccessibilityEngine.sleepEngine(2000);//等待

    AccessibilityEngine.mockEditId(widget.pages.shouye.shousuopinglunKuang,search); //赋值搜索框
    
   AccessibilityEngine.sleepEngine(2000);//等待

   //搜索按钮
    if (jixing == 1) {
        ScreenEngine.swipe(parseX(665), parseY(1219), parseX(665), parseY(1219), 500); //点击
    } else if (jixing == 2) {
		AccessibilityEngine.sleepEngine(5000);//等待
		//ScreenEngine.swipe(parseX(480), parseY(715), parseX(480), parseY(715), 500); //点击	
		ScreenEngine.swipe(parseX(700), parseY(1200), parseX(700), parseY(1200), 500); //点击
    }
  
    AccessibilityEngine.sleepEngine(1000);//等待
    if ( AccessibilityEngine.checkUpId(widget.pages.shousuoyemian.shousuomingzi) == ISTRUE ){
        AccessibilityEngine.mockOnClickIdIndex(widget.pages.shousuoyemian.shousuomingzi,0); // 点击第一个名字
    }else {
        showLog("搜索抖音号不存在");
        setTaskState("5", "搜索抖音号不存在");
        throw SyntaxError();
    }
    AccessibilityEngine.sleepEngine(1500);//等待
    if ( AccessibilityEngine.checkUpId(widget.pages.shousuoyemian.fenshiwaikuang) == ISTRUE){
        AccessibilityEngine.mockOnClickIdIndex(widget.pages.shousuoyemian.fenshiwaikuang,0); // 点击第一个名字
    }else {
        showLog("搜索抖音号不存在");
        setTaskState("5", "搜索抖音号不存在");
        throw SyntaxError();
    }

    AccessibilityEngine.sleepEngine(1000);//等待

    for  (var i =0 ; i <fanyeNum; i ++ ){ //翻几页
        ScreenEngine.swipe(parseX(300), parseY(1200), parseX(300), parseY(200), 1500); //从下往上滑动
    }
    showLog("issixin"+issixin);
    while ( fenSiNumF < fenSiNum){
        var mingZiNum= AccessibilityEngine.getIdNum(widget.pages.shousuoyemian.fensiwaimingziliebiao);
        // howLog("mingZiNum"+mingZiNum);
        for  (var i =0 ; i <mingZiNum; i ++ ){ //循环点击名字列表
            AccessibilityEngine.mockOnClickIdIndex(widget.pages.shousuoyemian.fensiwaimingziliebiao,i); // 循环点击名字列表
            AccessibilityEngine.sleepEngine(waitingTime*1000);//等待
            if ( issixin==0 ){
                if (AccessibilityEngine.checkUpId(widget.pages.shousuoyemian.fensiguanzu) == ISTRUE){
                    AccessibilityEngine.mockOnClickId(widget.pages.shousuoyemian.fensiguanzu); // 点击关注
					
					AccessibilityEngine.sleepEngine(3000);//等待
					if (AccessibilityEngine.checkUpText("对方已设置为私密帐号，需要对方通过请求后才能成功关注哦～") == ISTRUE  || AccessibilityEngine.checkUpIdText("android:id/message", "对方已设置为私密帐号，需要对方通过请求后才能成功关注哦～") == ISTRUE  || AccessibilityEngine.checkUpId("android:id/message") == ISTRUE ) { //私密账号 
						AccessibilityEngine.sleepEngine(2000);
						AccessibilityEngine.mockBack();
						AccessibilityEngine.sleepEngine(2000);
						AccessibilityEngine.mockBack();
						AccessibilityEngine.sleepEngine(2000);
						continue;
					}
					
					AccessibilityEngine.sleepEngine(3000);//等待  //已请求...
					if (AccessibilityEngine.checkUpText("已请求") == ISTRUE  || AccessibilityEngine.checkUpIdText("com.ss.android.ugc.aweme:id/c9g", "已请求") == ISTRUE  || AccessibilityEngine.checkUpId("com.ss.android.ugc.aweme:id/c9g") == ISTRUE ) { //私密账号 
						AccessibilityEngine.sleepEngine(2000);
						AccessibilityEngine.mockBack();
						AccessibilityEngine.sleepEngine(2000);
						AccessibilityEngine.mockBack();
						AccessibilityEngine.sleepEngine(2000);
						continue;
					}
					
                    AccessibilityEngine.sleepEngine(2000);//等待
                    if (AccessibilityEngine.checkUpId(widget.pages.shousuoyemian.fensiguanzu) == ISTRUE) {
                        AccessibilityEngine.mockOnClickId(widget.pages.shousuoyemian.guanzuyemianfanhui); // 点击返回
                        continue;
                    }
                    fenSiNumF++;
                    if (fenSiNumF >= fenSiNum){
                        showLog("执行成功");
                        setTaskState("5", "脚本执行完毕");
                        throw SyntaxError();
                    }
                }
                AccessibilityEngine.mockOnClickId(widget.pages.shousuoyemian.guanzuyemianfanhui); // 点击返回
            } else if (issixin==1){
                if (AccessibilityEngine.checkUpId(widget.pages.shousuoyemian.fensiguanzu) == ISTRUE) {
                    AccessibilityEngine.mockOnClickId(widget.pages.shousuoyemian.fensiguanzu); // 点击关注
					AccessibilityEngine.sleepEngine(3000);//等待
					if (AccessibilityEngine.checkUpText("对方已设置为私密帐号，需要对方通过请求后才能成功关注哦～") == ISTRUE  || AccessibilityEngine.checkUpIdText("android:id/message", "对方已设置为私密帐号，需要对方通过请求后才能成功关注哦～") == ISTRUE  || AccessibilityEngine.checkUpId("android:id/message") == ISTRUE ) { //私密账号 
						AccessibilityEngine.sleepEngine(2000);
						AccessibilityEngine.mockBack();
						AccessibilityEngine.sleepEngine(2000);
						AccessibilityEngine.mockBack();
						AccessibilityEngine.sleepEngine(2000);
						continue;
					}
					
					AccessibilityEngine.sleepEngine(3000);//等待  //已请求...
					if (AccessibilityEngine.checkUpText("已请求") == ISTRUE  || AccessibilityEngine.checkUpIdText("com.ss.android.ugc.aweme:id/c9g", "已请求") == ISTRUE  || AccessibilityEngine.checkUpId("com.ss.android.ugc.aweme:id/c9g") == ISTRUE ) { //私密账号 
						AccessibilityEngine.sleepEngine(2000);
						AccessibilityEngine.mockBack();
						AccessibilityEngine.sleepEngine(2000);
						AccessibilityEngine.mockBack();
						AccessibilityEngine.sleepEngine(2000);
						continue;
					}
                    AccessibilityEngine.sleepEngine(2000);//等待
                    if (AccessibilityEngine.checkUpId(widget.pages.shousuoyemian.fensiguanzu) == ISTRUE) {
                        AccessibilityEngine.mockOnClickId(widget.pages.shousuoyemian.guanzuyemianfanhui); // 点击返回
                        continue;
                    }
                    sendMessageFunction(sendMessage);
                    fenSiNumF++;
                    if (fenSiNumF >= fenSiNum){
                        showLog("执行成功");
                        setTaskState("5", "脚本执行完毕");
                        throw SyntaxError();
                    }
                }
                AccessibilityEngine.mockOnClickId(widget.pages.shousuoyemian.guanzuyemianfanhui); // 点击返回

            }
        }
        if (AccessibilityEngine.checkUpClazzText(widget.classText.widgetByClass.textView, "没有更多了~") == ISFALSE) {
            ScreenEngine.swipe(parseX(300), parseY(1200), parseX(300), parseY(200), 1500); //从下往上滑动
        }else {
            showLog("执行成功");
            setTaskState("5", "到底了");
            throw SyntaxError();
        }
    }
}

function sendMessageFunction(sendMessage){
    if (AccessibilityEngine.checkUpId(widget.pages.shousuoyemian.fensisixin) == ISTRUE){
       
        AccessibilityEngine.mockOnClickId(widget.pages.shousuoyemian.fensisixin); // 点击发消息
      
        AccessibilityEngine.mockOnClickId(widget.pages.fasongxiaoxi.fasongxiaoxikuang); // 点击发消息框
        AccessibilityEngine.sleepEngine(1000);//等待
        AccessibilityEngine.mockEditId(widget.pages.fasongxiaoxi.fasongxiaoxikuang, sendMessage); //消息框赋值
        AccessibilityEngine.sleepEngine(1000);//等待
        AccessibilityEngine.mockOnClickId(widget.pages.fasongxiaoxi.fasonganniu); // 点击发送按钮
		AccessibilityEngine.sleepEngine(500);//等待
        AccessibilityEngine.mockOnClickId(widget.pages.fasongxiaoxi.fanhui); // 点击返回
    }
}

var publicfunction = RequestEngine.getPluginPath() + "OS_DYCOMMON/publicfunction.js";
include(publicfunction); //公共方法js
var widgetPath = dyConditionCheck();
var widgetbyid = RequestEngine.getPluginPath() +"OS_DYCOMMON/"+ widgetPath;
include(widgetbyid);
main();